<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form onsubmit="return false">
        <textarea id="message" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="发送消息" onclick="send(this.form.message.value)">
        <textarea id="responseText" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="清空内容" onclick="document.getElementById('responseText').value('')">
    </form>
</body>
<script>
    var socket;
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:7000/hello");
        // 相当于channelRead0(), 收到服务器返回的消息
        var rt = document.getElementById('responseText');
        socket.onmessage = function (ev) {
            rt.value += ev.data + '\n';
        }

        // 连接开启
        socket.onopen = function (ev) {
            rt.value += '连接开启了...' + '\n';
        }
        // 连接关闭
        socket.onclose = function (ev) {
            rt.value += '连接关闭了...';
        }

        function send(message) {
            // 判断socket是否创建好
            if (!window.socket) {
                return;
            }
            if (socket.readyState == WebSocket.OPEN) {
                socket.send(message);
            } else {
                alert('连接未开启');
            }
        }
    } else {
        alert('当前浏览器不支持websocket');
    }
</script>
</html>